package com.k.kpp.config.datasource;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kkk
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    private Map<Object, Object> dataSourceMap = new HashMap<>();

    /**
     * 设置默认数据源  数据源 Map
     * @param defaultTargetDataSource default
     * @param targetDataSources       dataSourceMap
     */
    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
        this.dataSourceMap = targetDataSources;
        // lookupKey
        DynamicDataSourceContextHolder.addDataSourceKeys(targetDataSources.keySet());
    }


    /**
     * 如果不希望数据源在启动配置时就加载好，可以定制这个方法，从任何你希望的地方读取并返回数据源
     * 比如从数据库、文件、外部接口等读取数据源信息，并最终返回一个DataSource实现类对象即可
     */
    @Override
    protected DataSource determineTargetDataSource() {
        return super.determineTargetDataSource();
    }

    /**
     * 如果希望所有数据源在启动配置时就加载好，这里通过设置数据源Key值来切换数据，定制这个方法
     */
    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceContextHolder.getDataSourceKey();
    }


    /**
     * API For dataSource oper
     * @param lookupKey lookupKey
     * @param jdbcMap jdbcMap
     * @throws SQLException e
     */
    public void saveOrUpdateDataSource(String lookupKey, Map<String,Object> jdbcMap) throws SQLException {
        if(dataSourceMap == null || dataSourceMap.size() < 1 || dataSourceMap.get(lookupKey) == null){
            System.out.println("saveOrUpdateDataSource: add");
            DataSource ds = createDataSourceDruid(jdbcMap);
            dataSourceMap.put(lookupKey,ds);
            super.setTargetDataSources(dataSourceMap);
        } else {
            System.out.println("saveOrUpdateDataSource: update");
            DataSource ds = (DataSource) dataSourceMap.get(lookupKey);
            if(ds != null){
                resetDataSourceDruid(ds);
            }
        }
        super.afterPropertiesSet();
    }

    private DataSource createDataSourceDruid(Map<String,Object> jdbcMap){
        String jdbcUrl = (String) jdbcMap.get("jdbcUrl");
        String username = (String) jdbcMap.get("username");
        String password = (String) jdbcMap.get("password");
//        DruidDataSource ds = new DruidDataSource();
//        ds.setUrl(jdbcUrl);
//        ds.setUsername(username);
//        ds.setPassword(password);
//        return ds;
        return null;
    }

    private void resetDataSourceDruid(DataSource ds){
//        DruidDataSource dsDruid = (DruidDataSource) ds;
//        dsDruid.restart();
//        dsDruid.setUrl(jdbcUrl);
//        dsDruid.setUsername(username);
//        dsDruid.setPassword(password);
    }
}
